package edu.dhbw.mannheim;

import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;

public class Datastore {
	public static Entity getLatestPost() {
		// connect to the database
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Key blogKey = KeyFactory.createKey("blog", "blogblogblog");
		Query query2 = new Query("post", blogKey).addSort("date", Query.SortDirection.DESCENDING);
		List<Entity> posts = datastore.prepare(query2).asList(FetchOptions.Builder.withLimit(1));
		if(posts.size()==0) return null;
		return posts.get(0);
	}
	
	public static Entity getPost(int internalID) {
		// connect to the database
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Key blogKey = KeyFactory.createKey("blog", "blogblogblog");
		Query query2 = new Query("post", blogKey).addSort("date", Query.SortDirection.DESCENDING);
		List<Entity> posts = datastore.prepare(query2).asList(FetchOptions.Builder.withLimit(1));
		return posts.get(0);
	}
}
